from email.mime.text import MIMEText
from email.header import Header
import smtplib

from ..main import app


HOST = "smtp.sohu.com"
SENDER = 'linlinyue520@sohu.com'
RECEIVERS = ["xxx@xxx.com"]
USER = '*****'
PSW = "*****"


@app.task(bind=True, max_retries=3, default_retry_delay=60, ignore_result=True)
def send_email(self, recievers, title='', content=''):
    import time
    print('正在发送......')
    time.sleep(10)

    content = """
    it is a testing email
    it is a testing email
    it is a testing email
    it is a testing email
    """
    title = "SHILD ALARM TEST"
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['Subject'] = Header(title, 'utf-8')
    msg['From'] = 'linlinyue520@sohu.com'
    msg['To'] = ','.join(recievers)

    try:
        smtp_server = smtplib.SMTP()
        smtp_server.connect(HOST, 25)
        smtp_server.login(user=USER, password=PSW)
        smtp_server.sendmail(msg['From'], recievers, msg.as_string())
        smtp_server.quit()
        print('邮件发送成功')
        return
    except Exception as e:
        self.retry(exc=e)
        raise e


if __name__ == "__main__":
    send_email(recievers=[])
